package com.shanyutiwen.springbootblogtest.service;

import com.shanyutiwen.springbootblogtest.dao.*;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class OrderService {
    public static void main(String[] args) throws IOException {
        final InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        final SqlSession sqlSession = sqlSessionFactory.openSession(true);
        /*final OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
        final List<Order> all = orderMapper.findAll();
        for (Order o : all) {
            System.out.println(o);
        }*/

        final UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        final List<User> all = userMapper.findAllUserAndRole();

        /*for (User user : userList) {
            System.out.println(user.getUsername());
            for (Order order : user.getOrderList()) {
                System.out.println(order);
            }
        }*/
        for(com.shanyutiwen.springbootblogtest.dao.User user : all){
            System.out.println(user.getUsername());
            List<Role> roleList = user.getRoleList();
            for(Role role : roleList){
                System.out.println(role);
            }
            System.out.println("----------------------------------");
        }

    }
}
